import { _decorator, Component, Node, Prefab, loader, instantiate } from "cc";
const { ccclass, property } = _decorator;
import Constants from "../data/Constans";

// 生成弹窗：
// Constants.OnFire.emit(Constants.EventName.ADD_VIEW, { key: '弹窗的名字', value: { path: '自定义路径/true', } });

// 关闭弹窗
// Constants.OnFire.emit(Constants.EventName.CLOSE_VIEW, {key: 'pauseViewPrefab'});

// 关闭所有弹窗
// Constants.OnFire.emit(Constants.EventName.CLOSE_VIEW, {key: 'all'});

// 生成提示框（！！！！提示框不需要手动关闭！！！！）
// Constants.OnFire.emit(Constants.EventName.ADD_MESSAGE, { key: 'TipsMessage', value: { path: '自定义路径/true', text: '提示内容' } });

@ccclass("UIPopup")
export class UIPopup extends Component {
    private _popupArray = [];
    private _closedViewName = [];

    // 分发策略
    private _strategies: any = {
        // 提示框
        TipsMessage: (value) => { this._addPrefab('TipPrefab', value, 'TipPrefab'); },

        // 击败敌人提示框
        BeatTipMessage: (value) => { this._addPrefab('BeatTipMessagePrefab', value, 'BeatTipMessagePrefab'); },

        // 评论提示
        CommentMessage: (value) => { this._addPrefab('CommentMessagePrefab', value, 'CommentMessagePrefab'); },

        // 画剑弹窗
        StartGameView: (value) => { this._addPrefab('StartGameViewPrefab', value, 'StartGameViewPrefab'); },

        // AI匹配弹窗
        MatchiingView: (value) => { this._addPrefab('MatchingViewPrefab', value, 'MatchingViewPrefab'); },

        // 游戏开始倒计时
        StartPassView: (value) => { this._addPrefab('StartPassTipViewPrefab', value, 'StartPassTipViewPrefab'); },

        // 通关成功
        PassView: (value) => { this._addPrefab('PassViewPrefab', value, 'PassViewPrefab'); },

        // 通关失败
        FailView: (value) => { this._addPrefab('FailViewPrefab', value, 'FailViewPrefab'); },

        // 复活
        ReviveView: (value) => { this._addPrefab('ReviveViewPrefab', value, 'ReviveViewPrefab'); },

        // 视频分享
        ShareVideoView: (value) => { this._addPrefab('ShareVideoViewPrefab', value, 'ShareVideoViewPrefab'); },

        // 获取金币
        GetGoldView: (value) => { this._addPrefab('GetGoldViewPrefab', value, 'GetGoldViewPrefab'); },

        // 获取帮助
        HelpView: (value) => { this._addPrefab('HelpViewPrefab', value, 'HelpViewPrefab'); },

        // 商城
        StoreView: (value) => { this._addPrefab('StoreViewPrefab', value, 'StoreViewPrefab'); },

        // 武器加工厂
        ArmsVIew: (value) => { this._addPrefab('ArmsVIewPrefab', value, 'ArmsVIewPrefab'); },

        // 彩蛋
        EggsView: (value) => { this._addPrefab('EggsViewPrefab', value, 'EggsViewPrefab'); },

        // 试用未解锁皮肤
        TrySkillView: (value) => { this._addPrefab('TrySkillViewPrefab', value, 'TrySkillViewPrefab'); },

    }

    // 管理策略
    private _calculate(e) {
        this._strategies[e.key](e.value);
    }

    // 开启监听器
    onLoad() {
        // 注册展示弹窗
        Constants.OnFire.on(Constants.EventName.ADD_VIEW, this._addView.bind(this));

        // 注册轻提示弹窗
        Constants.OnFire.on(Constants.EventName.ADD_MESSAGE, this._addMessage.bind(this));

        // 关闭弹窗
        Constants.OnFire.on(Constants.EventName.CLOSE_VIEW, this._closeView.bind(this));
    }

    // 创建弹窗
    private _addView(event) {
        this._calculate(event);
    }

    // 创建轻提示
    private _addMessage(event) {
        this._calculate(event);
    }

    // 创建预制体
    private _addPrefab(prefabName, value, Script?: any) {
        for (let index = 0; index < this._popupArray.length; index++) {
            if (this._popupArray[index].name == prefabName) {
                return;
            }
        }

        let newNode = null;
        let url = null;
        if (value.path == 'true') {
            url = "prefab/tips/" + prefabName;
        } else {
            url = value.path + '/' + prefabName;
        }
        loader.loadRes(url, Prefab, (err: any, prefab: Prefab) => {
            if (!err) {
                newNode = instantiate(prefab);
                this.node.addChild(newNode);
                this._popupArray.push(newNode);

                if (Script) {
                    newNode.getComponent(Script).values = value;
                    newNode.getComponent(Script).init();
                }
            }
        })
    }

    // 关闭弹窗
    private _closeView(event) {
        if (event.key == 'all') {
            // 清除所有弹窗
            for (let index = 0; index < this.node.children.length; index++) {
                this.node.children[index].destroy();
            }
            this._popupArray = [];
        } else {
            // 关闭指定弹窗
            if (this._closedViewName.indexOf(event.key) === -1) {
                this._closedViewName.push(event.key);
            }

            for (let index = 0; index < this._closedViewName.length; index++) {
                for (let _index = 0; _index < this.node.children.length; _index++) {
                    if (this.node.children[_index].name == this._closedViewName[index]) {
                        this.node.children[_index].active = false;
                        break;
                    }
                }
            }

            this.scheduleOnce(() => {
                for (let index = 0; index < this._closedViewName.length; index++) {
                    for (let _index = 0; _index < this.node.children.length; _index++) {
                        if (this.node.children[_index].name == event.key) {
                            this.node.children[_index].destroy();
                            this._popupArray.splice(_index, 1);
                            this._closedViewName.splice(_index, 1);
                        }
                    }
                }

            }, 1.5);
        }
    }
}
